package com.cci.kangdao.crmapi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import com.cci.kangdao.utilTool.PropertiesUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class CRMGeocodeUtil {
	public static void geocode(Map<String, Object> map) {
		String address = (String) map.get("ADDRESS");
		try {
			address = URLEncoder.encode(address, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			address = "";
			e.printStackTrace();
		}
		//String key = "8e6018f14670bde77a241293bfb25e7e";
		//String url = "http://restapi.amap.com/v3/geocode/geo?address=" + address + "&key=" + key;
		String key = PropertiesUtil.getProperties("projectconfig.properties","Golden.GoldenKey");
		String url = PropertiesUtil.getProperties("goldenUrl.properties","geo");
		url = String.format("%s?address=%s&key=%s",url,address,key);
		JSONObject obj = JSONObject.fromObject(loadJSON(url));
		JSONArray myJsonArray = new JSONArray();
		if (obj != null && obj.containsKey("geocodes")){
			myJsonArray = JSONArray.fromObject(obj.get("geocodes").toString());
		}
		if (myJsonArray != null && myJsonArray.size() > 0) {
			String LAN_LON = myJsonArray.getJSONObject(0).getString("location");
			map.put("lat", LAN_LON.split(",")[1]);
			map.put("lon", LAN_LON.split(",")[0]);
		}else {

			String url2 = PropertiesUtil.getProperties("goldenUrl.properties","geo");
			url2 = String.format("%s?address=%s&key=%s",url2,"北京市东城区天安门",key);
			obj = JSONObject.fromObject(loadJSON(url2));
			if (obj != null && obj.containsKey("geocodes")){
				myJsonArray = JSONArray.fromObject(obj.get("geocodes").toString());
			}
			if (myJsonArray != null && myJsonArray.size() > 0) {
				String LAN_LON = myJsonArray.getJSONObject(0).getString("location");
				map.put("lat", LAN_LON.split(",")[1]);
				map.put("lon", LAN_LON.split(",")[0]);
			}else {
				map.put("lat", "");
				map.put("lon", "");
			}
		}
		
	}
	
	public static String geocode(String address) {
		String url = "http://restapi.amap.com/v3/geocode/geo?address=" + address + "&key=8e6018f14670bde77a241293bfb25e7e";
		return loadJSON(url);
	}
	
	public static String loadJSON (String url) {
        StringBuilder json = new StringBuilder();
        try {
            URL oracle = new URL(url);
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    yc.getInputStream()));
            String inputLine = null;
            while ( (inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
        return json.toString();
	}
	
	public static void main(String[] args) {
		try {
			JSONObject obj = JSONObject.fromObject(geocode("石家庄市开发区黄河大道225号"));
			System.out.println(obj);
			System.out.println(obj.get("geocodes"));
			JSONArray myJsonArray = JSONArray.fromObject(obj.get("geocodes").toString());
			System.out.println(myJsonArray.getJSONObject(0).getString("location"));
			
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ADDRESS", "石家庄市开发区黄河大道225号");
			
			geocode(map);
			System.out.println(map.get("lat") + "::::" + map.get("lon") );
//			String lngLat = obj.getString("location");  
//			System.out.println(lngLat);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
